package com.wc.算法基础课.C第三讲搜索与图论.Flood_Fill.动态网格;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/19 13:49
 * @description https://www.acwing.com/problem/content/description/645/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110;
    static char[][] g = new char[N][N];
    static boolean[][] st = new boolean[N][N];
    static int[] dx = new int[]{-1, 0, 1, 0};
    static int[] dy = new int[]{0, -1, 0, 1};
    static int t, r, c, x, y, q;
    static char z;

    public static void main(String[] args) {
        t = sc.nextInt();
        for (int step = 1; step <= t; step++) {
            out.printf("Case #%d:\n", step);
            r = sc.nextInt();
            c = sc.nextInt();
            for (int i = 0; i < r; i++) {
                g[i] = sc.next().toCharArray();
            }
            q = sc.nextInt();
            while (q-- > 0) {
                char op = sc.next().charAt(0);
                if (op == 'Q') {
                    for (int i = 0; i < r; i++) {
                        Arrays.fill(st[i], 0, c, false);
                    }
                    int res = 0;
                    for (int i = 0; i < r; i++) {
                        for (int j = 0; j < c; j++) {
                            if (g[i][j] == '1' && !st[i][j]) {
                                res++;
                                dfs(i, j);
                            }
                        }
                    }
                    out.println(res);
                } else {
                    x = sc.nextInt();
                    y = sc.nextInt();
                    z = sc.next().charAt(0);
                    g[x][y] = z;
                }
            }
        }
        out.flush();
    }

    static void dfs(int x, int y) {
        st[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            if (a >= 0 && a < r && b >= 0 && b < c && g[a][b] == '1' && !st[a][b]) {
                dfs(a, b);
            }
        }
    }

}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}